package com.yn.xgame.po;

import com.yn.xgame.CommonConfig;

/**
 * 网关信息
 * Created by wangboo on 2017/7/12.
 */
public class LoadInfo extends ServerInfo implements Comparable {

    public static final int STATE_WORKING = 1;
    public static final int STATE_CLOSING = 2;
    public static final int STATE_CLOSED = 3;

    private static final int MAX_AVERAGE = 10000;
    public int cur;
    public int max;
    public int state;

    public LoadInfo() {
    }

    public LoadInfo(int id, String ip, int port, String topic, int cur, int max, int state) {
        super(id, ip, port, topic);
        this.cur = cur;
        this.max = max;
        this.state = state;
    }

    public LoadInfo(CommonConfig config) {
        id = config.getId();
        ip = config.getInnerIp();
        port = config.getPort();
        topic = config.getTopic();
        cur = 0;
        max = config.getMaxUser();
        state = LoadInfo.STATE_WORKING;
    }

    public int average() {
        if (max == 0) return MAX_AVERAGE;
        return MAX_AVERAGE * cur/max;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof LoadInfo) {
            return average() - ((LoadInfo)o).average();
        }
        return 0;
    }

    public boolean isWorking() {
        return state == STATE_WORKING;
    }

    @Override
    public String toString() {
        return "LoadInfo{" +
                "id=" + id +
                ", cur=" + cur +
                ", max=" + max +
                ", state=" + state +
                '}';
    }
}
